package org.cqipc.edu.service.impl;

import org.cqipc.edu.common.Result;
import org.cqipc.edu.dao.UserDao;
import org.cqipc.edu.entity.User;
import org.cqipc.edu.service.UserService;
import org.cqipc.edu.utils.CommonUtils;
import org.cqipc.edu.vo.LoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @Description: 用户服务实现
 * @ClassNmae: UserServiceImpl
 * @Author: HeYue
 * @DateTime: 2020-10-30 21:01
 **/
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;


    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Object login(String username, String password, HttpSession session) {
        Result<User> result = new Result<>();
        if (userDao.queryUserNameAndPassword(username, password) != null) {
            session.setAttribute("loginInfo", userDao.queryUserNameAndPassword(username, password));
            return "success";
        }
        return "error";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object addUserInfo(LoginVO loginVO, HttpSession session) {
        User user = new User();
        BeanUtils.copyProperties(loginVO, user);
        user.setUserStatus(1).setUserType(1).setUserTime(CommonUtils.getCurrenDateTime());
        if (userDao.queryUserName(loginVO.getUserName()) != null) {
            return "cf";
        }
        return userDao.insertUser(user) ? "success" : "error";
    }
}
